<?php
include("../myzhuaqu.php");
require_once('../vendor/autoload.php');


$sourceData=get2lm(0,0,"");
$data=array();
for($i=0;$i<count($sourceData["data"]);$i++){
    $temp=array();
    $temp["id"]=$sourceData["data"][$i]["id"];
    $temp["name"]=$sourceData["data"][$i]["name"];
    $temp["trees"]=",".$temp["id"].",";
    rk($temp,0);
    for($j=0;$j<count($sourceData["data"][$i]["firstCategories"]);$j++){
        $temp2=array();
        $pid=$temp["id"];
        $temp2["id"]=$sourceData["data"][$i]["firstCategories"][$j]["id"];
        $temp2["name"]=$sourceData["data"][$i]["firstCategories"][$j]["name"];
        $temp2["trees"]=$temp["trees"].$temp2["id"].",";
        rk($temp2, $temp["id"]);
        get2lm($temp2["id"],$temp2["id"],$temp2["trees"]);
    }
}

function rk($data,$pid){
    $data["pid"]=$pid;
    $data["status"]=1;
    insertupdate("jipu.jipu_item_category_sph",$data,"select id from jipu.jipu_item_category_sph where name='{$data['name']}' and id='{$data['id']}'");
}

function get2lm($cid,$pid,$trees){
    $cid=$cid==0?0:"\"{$cid}\"";
    $poststr="{\"catIds\":[{$cid}]}";
    $len=strlen($poststr);
    $http2=<<<TEXT2
POST https://channels.weixin.qq.com/shop-faas/mmchannelstradecategory/cgi/batchGetSonCatJoinQuaNewCategory?token=&lang=zh_CN HTTP/1.1
Host: channels.weixin.qq.com
Connection: keep-alive
Content-Length: {$len}
sec-ch-ua: "Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"
Accept: application/json, text/plain, */*
Content-Type: application/json
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
sec-ch-ua-platform: "Windows"
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: https://channels.weixin.qq.com/shop/goods/category
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: cna=EYplHjnDiCsCATo73F+LXdnG; cookie2=1303c3a272abb05756d1f17dc0dd4b3a; arms_uid=191ee766-ea9f-4191-9ffb-99dfad83dda3; cookie1=WvFdKsGqAww2YuZPbLzfcke0SxSB9ipcvqQ0hLIXjYA%3D; lid=yzs1013; csg=90fb92ba; unb=40496108; sg=38f; xlly_s=1; ali_apache_tracktmp=c_w_signed=Y; cookie17=VyyVx2kq7XI%3D; ali_apache_track=c_mid=b2b-40496108|c_lid=yzs1013|c_ms=1; isg=BD8_62XrZoaZlWKEDP6ujlJXzhPJJJPG06S0E9EMwu414F9i2fDTF3_yJrAeuGs-; sgcookie=E100h5sJQyEDQL1JIGfXGgqQNqkkP1Eb%2BHrECWMlMJHuzPORquPO9wwLZHQSKstZeaTIh%2FY4H2SJ4EUHjUq8mF28QmuZ9rMzNLwTNCeWNsxeyoR5eaPh0QDGpDAYVy39igs6; t=6cb8dd5786fd552c3d2a031d6ca85c94; __cn_logon_id__=yzs1013; taklid=b5c313393e864476807feec2f18a2d93; __cn_logon__=true; _nk_=yzs1013; uc4=nk4=0%40GJ2FQQPI7120KJKe3cUIyjLU&id4=0%40VXtaSWpGBRARtNGshcz01Pgpxw%3D%3D; __mwb_logon_id__=yzs1013; JSESSIONID=5ECA88D757A042419BE8DAFEA664D66E; mwb=ng; _csrf_token=1710149083724; _m_h5_tk=60a4e48d7eb5f79f47a10d12720ada34_1710236061427; _tb_token_=e5aee15e316eb; x5sec=7b22733b32223a2238623330373764323833356336346233222c226c61707574613b32223a223030363664366333356235633933353861396266616461393939393135386638434c6149774b3847454a3764704e54352f2f2f2f2f774561436a51774e446b324d5441344f7a4577397179704b673d3d227d; mtop_partitioned_detect=1; _m_h5_tk_enc=f33bc64a4ed5469a93eed828d0f9cdb3; tfstk=eEYkqnjtj3S5VUz6MLQSo6XMiUixo8_ChpUdpwBE0tWXJ3BKV2chn6qULyRFKpvAhTKKObsen15g465FT-uV16VzUB8dTevfzMtRpTBh-B9iMA3tWQO5R9ktBVLirO5N8-FNcOOWNwNbMJo90Qa3ONoPxVDtpEyp7bjOI5I2Yub--M6waTRJwEWUBORPnyzFE_joMQW08y8c4Mr4bkC7RslpukZC4s1cBysb_ad9cLxIijqsVg5fwAhmikZC4s1cBjc0f8sPG_HO.; biz_token=b_0000018e_895db417_738533d4_f88bbe55_35d6ec27; biz_ticket=giclkNb1bYbBOvgBDBibvZ6QAAAAAAAAAAAAAAAAAAA=; biz_magic=0288979fcae82e7f2d7c1a2997b34aa8a80dda721bb38c973d8e5d0f0685559b; biz_rand=CAESIPQ5PF9m3N5R0zt7irbrNyaf3/9LzyrOd9wTD6CQkVHN

{$poststr}


TEXT2;

    $json=curlpost($http2);
    $sourceData=json_decode($json,1);
    $data=array();
    if(!empty($sourceData["productCats"])) {
        for ($i = 0; $i < count($sourceData["productCats"]); $i++) {
            $temp2 = array();
            $temp2["id"] = $sourceData["productCats"][$i]["id"];
            $temp2["name"] = $sourceData["productCats"][$i]["name"];
            $temp2["trees"] = $trees . $temp2["id"] . ",";
            rk($temp2, $pid);
            if (!$sourceData["productCats"][$i]["leaf"]) {
                get2lm($temp2["id"], $temp2["id"], $temp2["trees"]);
            }
        }
    }
}